﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using VQWEB.common;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for TinhthanhHandler
    /// </summary>
    public class hn_TinhthanhHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Tinh thanh
            string add_tinhthanh = context.Request["add_tinhthanh"];
            if (!string.IsNullOrEmpty(add_tinhthanh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_tinhthanh>(add_tinhthanh);
                context.Response.Write("Thực thi thành công !");
            }
            string view_tinhthanh = context.Request["view_tinhthanh"];
            if (!string.IsNullOrEmpty(view_tinhthanh))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_tinhthanh = context.Request["list_tinhthanh"];
            if (!string.IsNullOrEmpty(list_tinhthanh))
            {
                RenderList(context);
            }

            string delete_tinhthanh = context.Request["delete_tinhthanh"];
            if (!string.IsNullOrEmpty(delete_tinhthanh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.Delete<hn_tinhthanh>(delete_tinhthanh);
                RenderList(context);
            }

            string lockunlock_tinhthanh = context.Request["lockunlock_tinhthanh"];
            if (!string.IsNullOrEmpty(lockunlock_tinhthanh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_tinhthanh>(lockunlock_tinhthanh, "tinh_active");
                RenderList(context);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void RenderSingle(HttpContext context,int Id)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
         object obj = layer.Get<hn_tinhthanh>(
               t => t.tinh_id.Equals(Id),
               t => new
               {
                   id =t.tinh_id,
                   ma = t.tinh_ma,
                   mabuudien = t.tinh_mabuudien,
                   ten = t.tinh_ten,
                   active = t.tinh_active
               });
         string json = serializer.Serialize(obj);
         context.Request.ContentType = "application/json";
         context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            IList<object> list = layer.List<hn_tinhthanh>(
                null,
                null,
                null,
                t => t.tinh_ten,
                t => new
               {
                   id =t.tinh_id,
                   ma = t.tinh_ma,
                   mabuudien = t.tinh_mabuudien,
                   ten = t.tinh_ten,
                   active = t.tinh_active
               });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}